<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <meta http-equiv="X-UA-Compatible" content="ie=edge">
  <title>Document</title>
</head>

<body>
  <script>
    //数组原型方法（迭代-非破坏性-检测方法）thisValue可以指定callback中的this
    // Array.prototype.forEach(callback,thisValue?) //注意并不返回新的数组
    var arr1 = [2, 5, 8];
    // arr1.forEach(function (a,index,arr) { //值，下标，数组
    arr1.forEach(function (a) {
      if (a > 3) {
        console.log(a, "大于3");
      } else {
        console.log(a, "不大于3");
      }
    });
    console.log(arr1);



    // Array.prototype.every(callback,thisValue?) //返回一个布尔类型 若有不满足的将不再进行后续判断直接返回false
    var arr2 = [2, 5, 8]; //[2,4,6]
    var returnValue = arr2.every(function (a) { //判断数组元素是否都是偶数，若有不满足的将不再进行后续判断
      //console.log(a);//打开此行，查看是否会输出8，为什么？
      return a % 2 === 0;
    });
    console.log(returnValue);




    // Array.prototype.some(callback,thisValue?)//返回一个布尔类型 若有一部分满足的将不再进行后续判断，直接返回true
    var arr2 = [2, 5, 8]; //[2,4,6]
    var returnValue = arr2.some(function (a) { //判断数组元素是否都是偶数，若有满足的将不再进行后续判断
      //console.log(a);//打开此行，查看输出了哪些数，为什么？
      return a % 2 === 0;
    });
    console.log(returnValue);


    //Part2 数组原型方法（迭代-非破坏性-转换方法）
    // Array.prototype.map(callback,thisValue?) //Map映射 返回新的数组
    var arr2 = [1, 3, 5, 7, 9];
    var newArray = arr2.map(function (a) {
      return a * a;
    });
    console.log(newArray, arr2);



    // Array.prototype.filter(callback,thisValue?) //Filter过滤 返回新的数组
    var arr2 = [1, 3, 5, 7, 9];
    var newArray = arr2.filter(function (a) { //产生新数组，新数组的元素是返回为true的那些元素
      return (a > 2 && a < 8);
    });
    console.log(newArray, arr2);


    //Part3 数组原型方法（迭代-非破坏性-归约方法）
    // Array.prototype.reduce(element,initialValue?) //从左向右迭代
    // 对reduce的解读 ((((x1 op x2) op x3) op x4)...xn)
    // https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Global_Objects/Array/Reduce
    function add(prev, cur) {
      console.log(prev,cur);
      return prev + cur;
    }
    var arr3 = [10, 3, 1];
    console.log(arr3.reduce(add));
    // console.log(arr3.reduce(add,20));



    // Array.prototype.reduceRight(callback,initialValue?) //从右向左迭代
    function add(prev, cur) {
      console.log(prev,cur);
      return prev + cur;
    }
    var arr3 = [10, 3, 1];
    console.log(arr3.reduceRight(add));

    //综合实例
    function printArgs(prev, cur, i) {
      console.log("prev", prev, ",cur:", cur, ",i:", i);
      return prev + cur;
    }
    var arr4 = ["a", "b", "c", "d"];
    console.log(arr4.reduce(printArgs));
    console.log(arr4.reduce(printArgs, "x"));
    console.log(arr4.reduceRight(printArgs));
    console.log(arr4.reduceRight(printArgs, "x"));

    //思考案例：
    var flattened = [
      [0, 1],
      [2, 3],
      [4, 5]
    ].reduce(function (a, b) {
      return a.concat(b);
    });
    console.log(flattened);


    //思考案例：计算数组中每个元素出现的次数
    var names = ['Alice', 'Bob', 'Tiff', 'Bruce', 'Alice'];

    var countedNames = names.reduce(function (allNames, name) {
      if (name in allNames) {
        allNames[name]++;
      } else {
        allNames[name] = 1;
      }
      return allNames;
    }, {});
    // countedNames is:
    // { 'Alice': 2, 'Bob': 1, 'Tiff': 1, 'Bruce': 1 }
  </script>
</body>

</html>